<?
	session_start();	
	error_reporting(E_ALL);
	include('conn.php');	
	$id = $_REQUEST['id'];
	
	switch($id)
	{
		case '1':
			$qry1 = "SELECT distinct year(paydate) as yrs from payments";
			$qry = mysql_query($qry1);
			while($rows = mysql_fetch_array($qry))
				$data[] = $rows['yrs'];
			echo json_encode($data);
			break;
		
		case '2':			
			$yr = $_REQUEST['year'];
			$mon = $_REQUEST['month'];
			$qry1 = "SELECT p.socId,s.societyname,sum(p.payamount) as amtpaid 
				FROM payments p,society s where month(paydate)='$mon' and year(paydate)='$yr' and  
				s.societyname in (select societyname from society where s.socid=p.socId)
				group by socId;";
			$qry = mysql_query($qry1);
			$data = array();
			$i=1;
			while($rows = mysql_fetch_array($qry))
			{
				$qry2 = "select count(sc.TerminationDate) as ends from STBtoCustomer sc
						where sc.TerminationDate is not null and sc.CustId in 
						(select c1.custId from Customer c1 where c1.socid = '".$rows['socId']."') and month(sc.TerminationDate)='$mon' and year(sc.TerminationDate)='$yr';";
				$qry2 = mysql_query($qry2);
				$rows2 = mysql_fetch_array($qry2);
				
				$qry3 = "select count(sc.stbtocustid) as acts from STBtoCustomer sc 
						where month(sc.ActivationDate) <= '$mon' and year(sc.ActivationDate)<='$yr'
						and sc.TerminationDate is null and sc.CustId in (select c1.custId from Customer c1 where c1.socid = '".$rows['socId']."');";
				$qry3 = mysql_query($qry3);
				$rows3 = mysql_fetch_array($qry3);
				
				
				$data[] = array("Sr_NO" => $i++,
					"socid" => $rows['socId'],					 
					"soc" => $rows['societyname'],					 
					"amt" => $rows['amtpaid'],										
					"totcut" => $rows2['ends'],			
					"totconn" => $rows3['acts']				
				);
			}
			echo json_encode($data);
			break;
			
		default:
			echo "Please select an ID";
			break;
	}
?>